import Taro from '@tarojs/taro'
import app from '../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            displays: {},
            case_detail: {
                id: this.$router.params.id,
                visit_at: app.dateFormate(new Date(), "yyyy-MM-dd"), //就诊日期
                main_suit: '', //主诉
                jiwang_illness: '', //既往史
                present_illness:'', //现病史
                family_illness:'', //家族史
                diagnosis:'',//诊断
                cure:'', //治疗
            },
            template: [],
            result: {
                id: 'id1',
                type: '矩阵填空题',
                label: '基本病情',
                items: [{
                    label: '就诊日期',
                    name: 'visit_at',
                    type: 'date',
                    value: '',
                    options: [],
                }, {
                    label: '主诉',
                    name:'main_suit',
                    type: 'input',
                    value: '',
                    options: [],
                }, {
                    label: '既往史',
                    name: 'jiwang_illness',
                    type: 'select',
                    value: '',
                    options: ['无慢性病史2','糖尿病','高血压','心脏病','哮喘','其他慢性病','无眼外伤史','有眼外伤史','无戴镜','有戴镜','无眼部手术史','双眼白内障','右眼白内障','左眼白内障','双眼青光眼','右眼青光眼','左眼青光眼','双眼视网膜脱落','右眼视网膜脱落','左眼视网膜脱落','双眼激光矫正','其他眼病史','无传染病史','肝炎','结核','其他传染病史','无过敏史','头孢类过敏','磺胺药过敏','其他药物过敏']
                }, {
                    label: '现病史',
                    name: 'present_illness',
                    type: 'select',
                    value: '',
                    options: ['双眼渐进性视力下降','右眼渐进性视力下降','左眼渐进性视力下降','双眼偶有眼痒','干涩','右眼偶有眼痒','左眼偶有眼痒','双眼怕光','流泪','右眼怕光','左眼怕光','双眼眼球疼痛','右眼眼球疼痛','左眼眼球疼痛']
                }, {
                    label: '家族史',
                    name: 'family_illness',
                    type: 'select',
                    value: '',
                    options: ['无遗传性病史','有遗传性病史']
                }, {
                    label: '诊断',
                    name: 'diagnosis',
                    type: 'select',
                    value: '',
                    options: ['双眼年龄相关性白内障','右眼年龄相关性白内障','左眼年龄相关性白内障','双眼外伤性白内障','右眼外伤性白内障','左眼外伤性白内障','双眼先天性白内障','右眼先天性白内障','左眼先天性白内障','双眼并发性白内障','右眼并发性白内障','左眼并发性白内障','双眼翼状胬肉','右眼翼状胬肉','左眼翼状胬肉','双眼人工晶体眼','右眼人工晶体眼','左眼人工晶体眼','双眼屈光不正','右眼屈光不正','左眼屈光不正','双眼高度近视','右眼高度近视','左眼高度近视','双眼青光眼','右眼青光眼','左眼青光眼','双眼晶状体脱落','右眼晶状体脱落','左眼晶状体脱落','双眼剥落综合征','双眼剥落综合征','左眼剥落综合征','双眼虹膜松弛症','右眼虹膜松弛症','左眼虹膜松弛症','双眼眼底病','右眼眼底病','左眼眼底病','其他']
                }, {
                    label: '治疗',
                    name: 'cure',
                    type: 'select',
                    value: '',
                    options: ['PHACO','MS-MICS','SCIS']
                }]
            }
        };
        return this.state;
    },

    getCaseTemplate(name) {
        var me = this;
        app.get("api/case_templates/getByName", {name: name}, function(res) {
            if(res.data.result && res.data.result.displays) {
                var displays = {};
                var temp = res.data.result.displays.split(',');
                for(var i = 0; i < temp.length; i++) {
                    displays[temp[i]] = true;
                }
                me.setState({
                    displays: displays
                });
            }
        });
    },

    getBaseTemplate() {
        var me = this;
        app.get("web/case/base_template", {id: me.state.case_detail.id}, function(res) {
            var template = res.data.result;
            var result = [];
            for (var i = 0; i < template.length; i++) {
                if (template[i].id = 'id1') {
                    result = template[i];
                    break;
                }
            }
            me.setState({
                result: result
            });
        });
    },
    
    updateCase_detail() {
        var me = this;
        app.post("api/case_details/update", me.state.case_detail, function(res) {
            app.navigateBack('../editCase/editCase?id=' + me.state.case_detail.id);
        });
    },
    getCase_detail() {
        var me = this;
        app.get("api/case_details/get", {id: me.state.case_detail.id}, function(res) {
            var result = res.data.result;
            result.main_suit = !app.isEmpty(result.main_suit) ? result.main_suit : '';
            result.present_illness = !app.isEmpty(result.present_illness) ? result.present_illness : '';
            result.jiwang_illness = !app.isEmpty(result.jiwang_illness) ? result.jiwang_illness : '';
            result.chuanranbingshi = !app.isEmpty(result.chuanranbingshi) ? result.chuanranbingshi : '';
            result.guominshi = !app.isEmpty(result.guominshi) ? result.guominshi : '';
            result.family_illness = !app.isEmpty(result.family_illness) ? result.family_illness : '';
            result.diagnosis = !app.isEmpty(result.diagnosis) ? result.diagnosis : '';
            result.cure = !app.isEmpty(result.cure) ? result.cure : '';
            result.visit_at = !app.isEmpty(result.visit_at) ? result.visit_at : app.dateFormate(new Date(), 'yyyy-MM-dd');
            me.setState({
                case_detail: result
            });
        });
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    
    handleTextareaChange(name, event) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        
        temp[names[i]] = event.target.value;
        this.setState({n: temp});
    },
    handleDetailChange(name, event) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }

        temp[names[i]] = event.detail.value;
        this.setState({n: temp});
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        
        temp[names[i]] = v;
        this.setState({n: temp});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentWillMount() {
        this.init();
    },
    componentDidMount () {
        
    },
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
